/* *****************************************************************************************************
 * Copyright 2009. Dbwebx Group
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 * *****************************************************************************************************
 */
package org.dbwebx.service.dao.impl;

import org.dbwebx.domain.User;

import org.dbwebx.service.dao.UserService;
import org.dbwebx.service.dao.impl.base.BaseService;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * The {@link UserService} implementation. Provides methods to manage
 * {@link User}s.
 *
 * @author Izzet_Mustafayev
 *
 */
@SuppressWarnings( "unchecked" )
@Repository( "userService" )
@Transactional
public class UserServiceImpl
    extends BaseService
    implements UserService
{
    /*
     * (non-Javadoc)
     *
     * @see org.dbwebx.service.dao.UserService#find(java.lang.String)
     */
    public User find( final String login )
    {
        User foundUser = null;

        if ( null != login )
        {
            org.dbwebx.persistence.User user =
                (org.dbwebx.persistence.User) em.createNamedQuery( org.dbwebx.persistence.User.FIND_BY_LOGIN )
                                                .getSingleResult(  );

            if ( null != user )
            {
                foundUser = dozerMapper.map( user, User.class );
            }
        }

        return foundUser;
    }

    /*
     * (non-Javadoc)
     *
     * @see org.dbwebx.service.dao.UserService#list()
     */
    public List<User> list(  )
    {
        // TODO Auto-generated method stub
        return null;
    }

    /*
     * (non-Javadoc)
     *
     * @see org.dbwebx.service.dao.UserService#load(java.lang.Long)
     */
    public User load( final Long userId )
    {
        User user = null;

        if ( userId > 0 )
        {
            user =
                dozerMapper.map( em.find( org.dbwebx.persistence.User.class, userId ),
                                 User.class );
        }

        return user;
    }

    /*
     * (non-Javadoc)
     *
     * @see org.dbwebx.service.dao.UserService#persist(org.dbwebx.domain.User)
     */
    public void persist( final User user )
    {
        if ( null != user )
        {
            em.merge( dozerMapper.map( user, org.dbwebx.persistence.User.class ) );
        }
    }
}
